package stack;

public class ArrayStack {
    private int stack[];
    private int front = -1; // 转向栈顶
    private int maxSize;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isEmpty() {
        return front == -1;
    }

    public boolean isFull() {
        return front == maxSize - 1;
    }

    public void push(int val) {
        if(isFull()) {
            throw new RuntimeException("栈已满，不可再插入元素");
        }
        stack[++front] = val;
    }

    public int pop() {
        if(isEmpty()) {
            throw new RuntimeException("栈已空，不可再删除元素");
        }
        return stack[front--];
    }

    public int topVal() {
        if(isEmpty()) {
            throw new RuntimeException("栈已空，无元素");
        }
        return stack[front];
    }

    public void show() {
        for (int i = front; i >= 0; i--) {
            System.out.println(stack[i]);
        }
    }
}
